/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package share;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author mire
 */
public class FileCommunicator implements Communicator {

    private String fileName;
    private File file;
    private FileOutputStream fos;
    private FileInputStream fis;
    private BufferedOutputStream bos;
    private BufferedInputStream bis;
    private ObjectOutputStream oos;
    private ObjectInputStream ois;
    private boolean closed = true;
    private boolean reading;

    public FileCommunicator(String fileName) {
        this.fileName = fileName;
        this.file = null;
    }

    public FileCommunicator(File file) {
        this.file = file;
        this.fileName = file.getName();
    }

    @Override
    public boolean open() throws IOException {
        if (!closed) {
            return true;
        }
        if (file == null) {
            file = new File(fileName);
        }
        if (file.exists()) {
            reading = true;

            fis = new FileInputStream(fileName);
            bis = new BufferedInputStream(fis);
            ois = new ObjectInputStream(bis);
        } else {
            reading = false;
        }

        fos = new FileOutputStream(fileName);
        bos = new BufferedOutputStream(fos);
        oos = new ObjectOutputStream(bos);

        closed = false;

        return true;
    }

    @Override
    public boolean close() throws IOException {
        if (closed) {
            return true;
        }
        if (reading) {
            ois.close();
            bis.close();
            fis.close();
        }

        oos.close();
        bos.close();
        fos.close();

        closed = true;

        return true;
    }

    @Override
    public Object readObject() throws IOException, ClassNotFoundException {
        if (!reading) {
            return null;
        }
        return ois.readObject();
    }

    @Override
    public void writeObject(Object o) throws IOException {
        oos.writeObject(o);
        oos.flush();
    }

    @Override
    public String readString() throws IOException {
        if (!reading) {
            return null;
        }
        String s = null;
        try {
            s = (String) ois.readObject();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(FileCommunicator.class.getName()).log(Level.SEVERE, null, ex);
        }
        return s;
    }

    @Override
    public void writeString(String s) throws IOException {
        oos.writeObject(s);
        oos.flush();
    }

    @Override
    public int readInt() throws IOException {
        if (!reading) {
            return Integer.MIN_VALUE;
        }
        return ois.readInt();
    }

    @Override
    public void writeInt(int i) throws IOException {
        oos.writeInt(i);
        oos.flush();
    }
}
